﻿package net.area80.sitemanager.display
{
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.utils.Dictionary;
	
	/**
	* LoaderClip
	* @usage	File types supports are swf, jpg, png, gif
	* @author 	Wissarut Pimanmassuriya
	*/
	public class LoaderClip extends MovieClip
	{
		public var pathList:Dictionary;
		public var fileByPath:Dictionary;
		private var loadCount:int = 0;
		public var fileList:Array;
		/**
		 * Reset LoaderClip to blank
		 */
		public function resetLoader ():void {
			loadCount = 0;
			fileByPath = new Dictionary(true);
			pathList = new Dictionary(true);
			fileList = new Array();
		}
		public function addFile (path:String, type:String=""):void {
			var fileType:String = (type=="") ? (path.split("/")[path.split("/").length - 1].split("?")[0].split(".")[1]) : type;
			var fl:FileLoader = new FileLoader(path ,type);
			switch(fileType) {
				case "swf":
				case "jpg":
				case "png":
				case "gif":
				fl.file = new Loader();
				break;
				case "xml":
				default:
				fl.file = new URLLoader();
				break;
			}
			fileByPath[fileType] = path;
			pathList[path] = fl;
			fileList.push(fl);
		}
		
		public function startLoad ():void {
			var fl:FileLoader;
			var i:int;
			for (i = 0; i < fileList.length; i++) {
				fl = fileList[i] as FileLoader;
				switch(fl.type) {
					case "swf":
					case "jpg":
					case "png":
					case "gif":
					loadClip(Loader(fl.file));
					
					break;
					case "xml":
					default:
					fl.file = new URLLoader();
					break;
				}
			}
		}
		
		public function getFileByPath (path:String):Object {
			return FileLoader(pathList[path]).file;
		}
		
		public function getPathFromFile (file:Object):String {
			return fileByPath[file] as String;
		}
		
		private function loadClip (file:Loader):void {
			file.load(new URLRequest(getPathFromFile(file)));
			file.addEventListener(Event.INIT, initHandler, false, 0, true);
			file.addEventListener(Event.COMPLETE, completedHandler, false, 0, true);
			file.addEventListener(ProgressEvent.PROGRESS, progressHandler, false, 0, true);
			file.addEventListener(IOErrorEvent.IO_ERROR, errorHandler, false, 0, true);
		}
		private function loadData (file:URLLoader):void {
			
		}
		private function initHandler (e:Event):void {
			
		}
		private function completedHandler (e:Event):void {
			
		}
		
		private function progressHandler (e:Event):void {
			
		}
		private function errorHandler (e:Event):void {
			if (e.target is Loader){
				loadClip(Loader(e.target));
			} else if (e.target is URLLoader) {
				loadData(URLLoader(e.target));
			}
		}
		
	}
}
internal class FileLoader
{
		internal var type:String;
		internal var file:Object;
		internal var path:String;
		
		public function FileLoader (path:String, type:String):void {
				this.path = path;
				this.type = type;
		}

}